package com.room.controller.announ;

import com.room.pojo.Announ;
import com.room.pojo.DormResult;
import com.room.pojo.Student;
import com.room.service.AnnounService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 lin
 * @创建时间 2019/10/5
 * @描述
 */
@RestController
@RequestMapping("/annout/manage")
public class AnnounController {
    @Autowired
    AnnounService announService;


    /**
     * 添加公告
     * @param announ
     * @return
     */
    @RequestMapping(value="/insertAnnoun",method = RequestMethod.POST)
    public DormResult insertAnnoun(Announ announ){
        announ.setTime(new Date());
        if(announService.insertAnnoun(announ)==1){
            return DormResult.ok("添加公告成功");
        }
        return DormResult.error("添加公告失败");
    }
    /**
     * 根据分页参数返回公告数据
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(value = "/annout",method = RequestMethod.GET)
    public Map<String,Object> getAnnounByPage(
            @RequestParam(defaultValue = "1")Integer page,
            @RequestParam(defaultValue = "6")Integer size
    ){
        Map<String,Object> map = new HashMap<>();
      List<Announ> announs = announService.getAnnounByPage(page, size);
        Long total = announService.getAnnounCount();
        map.put("announs",announs);
        map.put("total",total);
        return map;
    }


    /**
     * 修改公告内容
      * @param announ
     * @return
     */
    @RequestMapping(value = "/annout",method = RequestMethod.PUT)
    public DormResult updateAnnoun(Announ announ){
        if(announService.updateAnnoun(announ)==1) {
            return DormResult.ok("更新成功");
        }
        return DormResult.error("更新失败");
    }

    /**
     * 删除指定id的公告数据
     * @param ids
     * @return
     */
    @RequestMapping(value = "/annout/{ids}",method = RequestMethod.DELETE)
    public DormResult deleteAnnounById(@PathVariable String ids){
        if(announService.deleteAnnounById(ids)){
            return DormResult.ok("删除成功");
        }
        return DormResult.error("删除失败");
    }
}
